package ${filePackageName};

import cn.hutool.core.util.ObjectUtil;
import ${beanCommonPackage}.QueryParam;
import ${beanCommonPackage}.Wrapper;

/**
 * sql语句操作类
 * @author ${projectAuthor}
 * @version 1.0
 * @date ${nowDate}
 */
public class SQLUtil {

    public static String sortSQL(QueryParam queryParam){
        return " order by " + queryParam.getSortName() + " " +
                (ObjectUtil.isNotEmpty(queryParam.getSortOrder())?queryParam.getSortOrder():"");
    }

    public static String sortSQL(String sortName, String sortOrder){
        return (ObjectUtil.isNotEmpty(sortName) ? " order by " + sortName + " ":"")
                + (ObjectUtil.isNotEmpty(sortOrder)?sortOrder:"");
    }

    public static String sortSQL(Wrapper wrapper){

        boolean sortNameFlag = ObjectUtil.isNotEmpty(wrapper) && ObjectUtil.isNotEmpty(wrapper.getSortName());
        boolean sortOrderFlag = ObjectUtil.isNotEmpty(wrapper) && ObjectUtil.isNotEmpty(wrapper.getSortOrder());

        return (sortNameFlag ? " order by " + wrapper.getSortName() + " ":"")
                + (sortOrderFlag?wrapper.getSortOrder():"");
    }

    public static String likeSQL(String columName,String value){
        return " "+columName+" LIKE concat('%', '"+value+"', '%') ";
    }
    public static String jsonObjectLikeSQL(String columName,String objName,String value){
        return " "+columName+"->'$."+objName+"' LIKE concat('%', '"+value+"', '%') ";
    }


    public static String jsonArrayLikeSQL(String columName,String value){
        return " "+columName+"->'$[*]' LIKE concat('%', '"+value+"', '%') ";
    }

    public static String dateTimeEndSQL(String columName,String dateTime){
        return " date_format ("+columName+",'%Y-%m-%d %H:%i:%s') <= date_format('" + dateTime + "','%Y-%m-%d %H:%i:%s')";
    }

    public static String dateTimeStartSQL(String columName,String dateTime){
        return " date_format ("+columName+",'%Y-%m-%d %H:%i:%s') >= date_format('" + dateTime + "','%Y-%m-%d %H:%i:%s')";
    }

    /**
     * 字符串转化 添加单引号  例如 a,b,c 转化成 'a','b','c'
     * @param str
     * @return
     */
    public static String inCharSQL(String  colunm, String str){
        String newStr = "";
        if(ObjectUtil.isNotEmpty(str)){
            if("'".equals(str.substring(0,1)) &&
                    "'".equals(str.substring(str.length()-1))){
                return colunm +" in ("+str + ")";
            }

            String [] array = str.split(",");
            for(int i=0;i<array.length;i++){
                if(i==0){
                    newStr+="'"+array[i]+"'";
                }else{
                    newStr+=",'"+array[i]+"'";
                }
            }
        }
        return colunm +" in ("+newStr + ")";
    }

    /**
     * 数组转单引号字符串
     * @param array
     * @return
     */
    public static String inArraySQL(String colunm, String ... array){
        String newStr = "";
        if(array!=null && array.length>0){
            for(int i=0;i<array.length;i++){
                if(i==0){
                    newStr+="'"+array[i]+"'";
                }else{
                    newStr+=",'"+array[i]+"'";
                }
            }
        }
        return colunm +" in ("+newStr + ")";
    }

    /**
     * 字符串转化 添加单引号  例如 a,b,c 转化成 'a','b','c'
     * @param str
     * @return
     */
    public static String str2CharAndQuot(String str){
        String newStr = "";
        if(ObjectUtil.isNotEmpty(str)){
            String [] array = str.split(",");
            for(int i=0;i<array.length;i++){
                if(i==0){
                    newStr+="'"+array[i]+"'";
                }else{
                    newStr+=",'"+array[i]+"'";
                }
            }
        }
        return newStr;
    }

    /**
     * 数组转单引号字符串
     * @param array
     * @return
     */
    public static String array2CharStr(String ... array){
        String newStr = "";
        if(array!=null && array.length>0){
            for(int i=0;i<array.length;i++){
                if(i==0){
                    newStr+="'"+array[i]+"'";
                }else{
                    newStr+=",'"+array[i]+"'";
                }
            }
        }
        return newStr;
    }

}

